from rest_framework import serializers
from rest_framework.settings import api_settings

from toutiao.models import NewsCategory, News, NewContent


class NewCategorySerializer(serializers.ModelSerializer):
    '''新闻类别序列化器'''
    id = serializers.IntegerField(label='id', read_only=True)
    name = serializers.CharField(label='名称', max_length=10)

    class Meta:
        model = NewsCategory
        fields = ('id', 'name')


class NewsListSerializer(serializers.ModelSerializer):

    id = serializers.IntegerField(label='id', read_only=True)
    # user_name = serializers.StringRatedField(label='发布者',required=True)
    title = serializers.CharField(label='标题', read_only=True)
    category_id = serializers.SlugRelatedField(label='新闻类别', read_only=True, slug_field='name')
    create_time = serializers.DateTimeField(label='发布时间', required=True)
    content = serializers.CharField(label='新闻正文', required=True)
    digest = serializers.CharField(label='新闻摘要',read_only=True)
    user_id = serializers.SlugRelatedField(label='发布者',read_only=True,slug_field='username')
    class Meta:
        model = News
        fields = ('id', 'title', 'create_time', 'content', 'category_id','digest','user_id')


class NewsSerializer(serializers.ModelSerializer):
    '''新闻序列化器'''
    id = serializers.IntegerField(label='id', read_only=True)
    # user = serializers.CharField(label='发布者',required=True)
    title = serializers.CharField(label='标题', read_only=True)
    category_id = serializers.SlugRelatedField(label='新闻类别', read_only=True, slug_field='name')
    create_time = serializers.DateTimeField(label='发布时间', required=True)
    content = serializers.CharField(label='新闻正文', required=True)

    def create(self, validated_data):
        # 创建新闻
        pass

    class Meta:
        model = News
        fields = ('id', 'title', 'create_time', 'content', 'category_id')


class NewContentSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(label='广告id',read_only=True)
    new_content_image = serializers.ImageField(label='广告图片',read_only=True)

    class Meta:
        model = NewContent
        fields = ('id','image')


class submitNewsSerializer(serializers.ModelSerializer):
    """发布头条序列化器"""
    title = serializers.CharField(label='标题',min_length=1,write_only=True)
    content = serializers.CharField(label='头条内容',min_length=1,write_only=True)
    user_id = serializers.IntegerField(label='作者id')

    class Meta:
        model = News
        fields = ('title','content','category_id','user_id')

